﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.sunface.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class SunFaceViewMediator extends Mediator implements ITicking {

		[Inject]
        public var view:SunFaceView;
		[Inject]
        public var injector:IInjector;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var tutorialModel:TutorialModel;
        private var _sunFaceStateManager:SunFaceStateManager;
        private var _changeRatio:Number;

        private static const IDLE_PLAYING_CHANCE_0_to_100:Number = 30;

        override public function preRegister():void{
            eventMap.mapListener(eventDispatcher, EventConstants.INIT_WORLD, this.onInitWorldComplete);
            this._sunFaceStateManager = this.injector.getInstance(SunFaceStateManager);
            this._sunFaceStateManager.initialize(this.view.getEmbeddedAssetMovieClip());
            super.preRegister();
        }
        override public function onRegister():void{
            super.onRegister();
            eventMap.mapListener(eventDispatcher, GameConstants.SUN_START_TALKING, this.startToTalk);
            eventMap.mapListener(eventDispatcher, GameConstants.SUN_STOP_TALKING, this.stopToTalk);
            eventMap.mapListener(eventDispatcher, GameEvent.TUTORIAL_STATE_CHANGE, this.tutorialStateChange);
            eventMap.mapListener(this.view, MouseEvent.MOUSE_UP, this.onSunFaceViewClicked);
            if (this.isGameInTutorialModus()){
                this._sunFaceStateManager.currentState = this._sunFaceStateManager.getStateByLookupKey(SunFaceStateManager.DAY_STATE);
                this._sunFaceStateManager.disableStateUpdates();
            };
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.MINUTE));
        }
        private function tutorialStateChange(_arg1:GameEvent):void{
            if (this.tutorialModel.state > TutorialModel.END){
                this._sunFaceStateManager.enableStateUpdates();
            };
        }
        private function get chanceRatio_0_to_1():Number{
            return ((this._changeRatio = ((this._changeRatio) || ((IDLE_PLAYING_CHANCE_0_to_100 / 100)))));
        }
        private function onInitWorldComplete(_arg1:Event):void{
            this.view.visible = true;
        }
        private function onSunClick(_arg1:Event):void{
            this._sunFaceStateManager.idle();
        }
        private function isGameInTutorialModus():Boolean{
            return ((this.gameStateModel.state == GameStateModel.STATE_GAME_TUTORIAL));
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            this._sunFaceStateManager.updateTimeBaseDefaultState(new Date().hours);
            if (this._sunFaceStateManager.timebaseStateNeedsUpdating()){
                this._sunFaceStateManager.currentState = this._sunFaceStateManager.defaultState;
                if (this._sunFaceStateManager.isTalking()){
                    this._sunFaceStateManager.talk();
                };
            } else {
                if (this.idleStateAvailableBasedOnChance()){
                    this._sunFaceStateManager.idle();
                };
            };
        }
        private function idleStateAvailableBasedOnChance():Boolean{
            return ((Math.random() <= this.chanceRatio_0_to_1));
        }
        private function onSunFaceViewClicked(_arg1:MouseEvent):void{
            this._sunFaceStateManager.click();
        }
        public function startToTalk(_arg1:Event):void{
            this._sunFaceStateManager.talk();
        }
        public function stopToTalk(_arg1:Event):void{
            this._sunFaceStateManager.stop();
        }

    }
}//package net.wooga.pets.views 
